from mrjob.job import MRJob
from mrjob.step import MRStep
import json
import re


class JobMRJob(MRJob):
    def steps(self):
        return [
            MRStep(mapper_raw=self.read_ini_file,
                   # mapper_init = self.mapper1_init,
                   # mapper_final = self.mapper1_final,
                   reducer=self.reducer1
                   ),
        ]

    def read_ini_file(self, wet_path, wet_uri):
        job_info = {}
        job_name = None
        jobs = []

        with open(wet_path, "r", encoding="utf-8") as fp:
            while True:
                line = fp.readline()
                if not line:
                    job_info.update({"job": job_name})
                    jobs.append(job_info)
                    break

                m = re.match('^\[(\w+)\]$', line)
                if m:
                    if job_name:
                        job_info.update({"job": job_name})
                        jobs.append(job_info)
                        job_info = {}

                    job_name = m.group(1)
                    continue

                m = re.match('^(\w+)=(.+)$', line)
                if m:
                    job_info[m.group(1)] = m.group(2)
                    continue

        for job in jobs:
            try:
                address = job['address']
                m = re.match('(?P<min_salary>[\d\.]+)-(?P<max_salary>[0-9\.]+)(?P<unit>(万|千))/(?P<time>(月|年))',
                             job['salary'])
                if not m:
                    continue

                min_salary = float(m.group("min_salary"))
                max_salary = float(m.group("max_salary"))

                if m.group("unit") == "万":
                    min_salary *= 10
                    max_salary *= 10

                if m.group("time") == "年":
                    min_salary /= 12
                    max_salary /= 12

                avg_salary = (min_salary + max_salary) / 2
                yield (address, avg_salary)
            except Exception as e:
                pass


    def reducer1(self, key, values):
        values = list(values)

        avg_salary = sum(values) / len(values)
        yield (key, (avg_salary, len(values)))


if __name__ == "__main__":
    JobMRJob.run()
